package cn.itsource.zcn.pet.controller;

import cn.itsource.zcn.org.domain.Shop;
import cn.itsource.zcn.org.query.ShopQuery;
import cn.itsource.zcn.pet.domain.Pet;
import cn.itsource.zcn.pet.query.PetQuery;
import cn.itsource.zcn.pet.service.IPetService;
import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * (Pet)表控制层
 *
 * @author makejava
 * @since 2021-08-23 11:32:21
 */
@RestController
@RequestMapping("/pet")
public class PetController {
    /**
     * 服务对象
     */
    @Autowired
    private IPetService tPetService;

    /**
     * 查询全部数据
     *
     * @return List<>
     */
    @RequestMapping("/all")
    public List<Pet> queryAll() {
        return tPetService.queryAll();
    }


    /**
     * 添加或者修改
     * @return AjaxResult
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Pet pet, HttpServletRequest request) {
        try{
            if(pet.getId()!=null){
                tPetService.update(pet);
            }else{
                tPetService.save(pet,request);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
     * 删除数据
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            tPetService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 查询一条数据
     * @return List<>
     */
    @GetMapping("/{id}")
    public Pet queryById(@PathVariable("id") Long id) {
        return tPetService.queryById(id);
    }

    /**
     * 查询一条数据
     * @return List<>
     */
    @GetMapping("/detailfront/{id}")
    public Pet queryByIdfront(@PathVariable("id") Long id) {
        return tPetService.queryByIdfront(id);
    }

    /**
     * 查询高级+分页数据
     * @return QueryResult
     */
    @PostMapping
    public QueryResult queryPage(@RequestBody PetQuery petQuery, HttpServletRequest request) {
//        System.out.println(shopQuery);
        return tPetService.queryPage(petQuery,request);
    }

    @PostMapping("/front")
    public QueryResult queryPagefront(@RequestBody PetQuery petQuery) {
//        System.out.println(shopQuery);
        return tPetService.queryPage(petQuery);
    }

    /**
     * 批量删除
     * @return AjaxResult
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody  Long[] ids) {
        try {
            tPetService.deleteBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }
    /**
     * 批量上架
     * @return AjaxResult
     */
    @PatchMapping("/onsale")
    public AjaxResult patchOnsale(@RequestBody  Long[] ids) {
        try {
            tPetService.onsaleBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }
    /**
     * 批量下架
     * @return AjaxResult
     */
    @PatchMapping("/offsale")
    public AjaxResult patchOffsale(@RequestBody  Long[] ids) {
        try {
            tPetService.offsaleBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

}
